using Microsoft.AspNetCore.Mvc;
using ZeroSum.Domain.Datas;
using ZeroSumDBM.Application.Services.DataServers;
using ZeroSumDBM.Application.Services.DataServers.Dto.In;
using ZeroSumDBM.Application.Services.DataServers.Dto.Out;
using ZeroSumDBM.Application.Services.DataServers.Dto.Query;
using ZeroSumDBM.Core.MySql;

namespace ZeroSumDBM.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    public class DataServerController : ControllerBase
    {
        private readonly IDataServerService _service;
        public DataServerController(IDataServerService service)
        {
            _service = service;
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<ListDataServerDto>>> List([FromQuery] ListDataServerQuery query)
        {
            var entities = await _service.List(query);
            return ApiResponse<List<ListDataServerDto>>.Of(entities);
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse> Add([FromBody] AddDataServerDto dto)
        {
            var connection = MysqlHelper.TestConnection(dto.Host, dto.Account, dto.Password, dto.Port);
            if (!connection)
            {
                return ApiResponse.NotOk("数据库连接失败！");
            }
            var dataBases = MysqlHelper.ShowDataBase(dto.Host, dto.Account, dto.Password, dto.Port);
            await _service.Add(dto);
            return ApiResponse.Ok();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
#pragma warning disable CS1998 // 此异步方法缺少 "await" 运算符，将以同步方式运行。请考虑使用 "await" 运算符等待非阻止的 API 调用，或者使用 "await Task.Run(...)" 在后台线程上执行占用大量 CPU 的工作。
        public async Task<ApiResponse<bool>> Test([FromBody] AddDataServerDto dto)
#pragma warning restore CS1998 // 此异步方法缺少 "await" 运算符，将以同步方式运行。请考虑使用 "await" 运算符等待非阻止的 API 调用，或者使用 "await Task.Run(...)" 在后台线程上执行占用大量 CPU 的工作。
        {
            var result = MysqlHelper.TestConnection(dto.Host, dto.Account, dto.Password, dto.Port);
            return ApiResponse.Of(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse> Delete([FromBody] DeleteDataServerDto dto)
        {
            await _service.Delete(dto);
            return ApiResponse.Ok();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse> Edit([FromBody] EditDataServerDto input)
        {
            await _service.Edit(input);
            return ApiResponse.Ok();
        }

        /// <summary>
        /// 查看用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<DetailDataServerDto>> Detail([FromQuery] DetailDataServerQuery query)
        {
            var detail = await _service.Detail(query);
            return ApiResponse<DetailDataServerDto>.Of(detail);
        }

        /// <summary>
        /// 获取某个库下所有databases
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<string>>> Databases([FromQuery] long id)
        {
            var list = await _service.GetDatabases(id);
            return ApiResponse<List<string>>.Of(list);
        }

        /// <summary>
        /// 获取单个database下所有table
        /// </summary>
        /// <param name="id"></param>
        /// <param name="database"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<string>>> Tables([FromQuery] long id, [FromQuery] string database)
        {
            var list = await _service.GetTables(id, database);
            return ApiResponse<List<string>>.Of(list);
        }


        /// <summary>
        /// 获取table的data
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<SearchResult>> Data([FromQuery] TableDataQuery query)
        {
            var data = await _service.GetTableData(query);
            return ApiResponse<SearchResult>.Of(data);
        }
    }
}